﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MatchCore
{
    public class Bullet
    {
        private Vector2 _position;
        private Player _player;
        private int _speed;
        private uint _startFrameId;
        private int _startX;
        private int _damageValue;

        private IBulletEventer _bulletEventer;

        public Bullet(Player player,int speed,int damageValue)
        {
            this._player = player;
            this._speed = speed;
            this._position = player.GetCurShootPosition();
            this._startFrameId = LogicManager.Instance().GetCurFrameId();
            this._startX = this._position.x;
            this._damageValue = damageValue;

            LogicManager.Instance().LogDebug("bullet pos:(" + this._position.x + "," + this._position.y + ")");
        }

        public void SetBulletEventer(IBulletEventer bulletEventer)
        {
            _bulletEventer = bulletEventer;
        }

        public Vector2 GetPosition()
        {
            return _position;
        }

        public int GetSpeed()
        {
            return this._speed;
        }

        public int GetDamageValue()
        {
            return _damageValue;
        }

        public bool HitTest(Player player)
        {
            if(player != this._player)
            {
                return player.HitTest(_position);
            }
            return false;
        }

        public void Update(uint frameId)
        {
            _position.x = _startX + (int)(_speed * (frameId - _startFrameId));
        }

        public void OnHitPlayer(Player player)
        {
            try
            {
                if (_bulletEventer != null)
                {
                    _bulletEventer.OnBulletHitPlayer(this, player);
                }
            }
            catch(Exception ex)
            {
                LogicManager.Instance().LogException("call OnBulletHitPlayer ex:" + ex);
            }
        }

        public void OnDestroy()
        {
            try
            {
                if (_bulletEventer != null)
                {
                    _bulletEventer.OnBulletDestroy(this);
                }
            }
            catch (Exception ex)
            {
                LogicManager.Instance().LogException("call OnBulletDestroy ex:" + ex);
            }
        }
    }
}
